package com.dhecp.project.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.project.business.domain.LogisticsMmodeGrads;
import com.dhecp.project.logstics.mapper.LogsticsMmodeGradsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysDepotInfo;
import com.dhecp.project.system.mapper.SysDepotInfoMapper;
import com.dhecp.project.system.service.SysDepotInfoService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 仓库信息	服务实现层
 * @author DH-lwl
 * @version 2019年10月28日
 */
@Service
public class SysDepotInfoServiceImpl implements SysDepotInfoService {

	@Autowired
	private SysDepotInfoMapper depotInfoMapper;

	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;

	@Autowired
	private LogsticsMmodeGradsMapper logisticsMmodeGradsMapper;
	
	/**
	 * 查询所有仓库信息
	 * @param params
	 * @return List-SysDepotInfo
	 * @author lwl
	 * @throws Exception 
	 */
	@Override
	public List<SysDepotInfo> queryDepotInfo(Map<String, Object> params) throws Exception {
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));
		PageUtils.startPage();
		return depotInfoMapper.findDepotInfo(params);
	}
	
	/**
	 * 通过guid查询单条记录
	 * @param guid
	 * @return SysDepotInfo
	 * @author lwl
	 */
	@Override
	public SysDepotInfo queryDepotInfoByGuid(String guid) {
		return depotInfoMapper.findDepotInfoByGuid(guid);
	}
	
	/**
	 * 新增仓库信息
	 * @param depotInfoItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	@Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
	public AjaxResult insertDepotInfo(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		SysDepotInfo depotInfo = JSON.parseObject(params.get("depotInfoItems").toString(), SysDepotInfo.class);


		if (depotInfo == null ) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		int count = depotInfoMapper.countByCode(depotInfo);
		if (count != 0){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return AjaxResult.error("仓库编码重复,请核实再操作！");
		}
		String guid = RandomValueUtils.getGUID();
		depotInfo.setGuid(guid);
		depotInfo.setCreateUser(ShiroUtils.getUserId());
		depotInfo.setCreateTime(new Date());

		depotInfoMapper.insertDepotInfo(depotInfo);

		//判断仓储费梯度列表是否正确
		List<LogisticsMmodeGrads> infoItemsZj = JSON.parseArray(params.get("infoItems").toString(), LogisticsMmodeGrads.class);
		List<LogisticsMmodeGrads> infoItemsList;
		List<LogisticsMmodeGrads> infoItemsList1;
		for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
			if (logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads.getEndSection()) >= 0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return AjaxResult.error("开始值不允许小于等于结束值");
			}
			infoItemsList = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
					&& logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
					&& !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
			).collect(Collectors.toList());
			infoItemsList1 = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
					&& logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
					&& !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
			).collect(Collectors.toList());
			if (infoItemsList.size() != 0 || infoItemsList1.size() != 0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return AjaxResult.error("区间有重叠");
			}
		}

		List<LogisticsMmodeGrads> logisticsMmodeInfoItemsList = new ArrayList<>();
		for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
			logisticsMmodeGrads.setGuid(RandomValueUtils.getGUID());
			logisticsMmodeGrads.setLogisticsMmodeGuid(guid);
			logisticsMmodeGrads.setType("CCFTD");
			logisticsMmodeInfoItemsList.add(logisticsMmodeGrads);
		}
		//新增仓储费梯度
		logisticsMmodeGradsMapper.insertLogisticsMmodeGradsBatch(logisticsMmodeInfoItemsList);
		return AjaxResult.success(MessageUtils.message("module.success.issuccess"),guid);
	}

	/**
	 * 删除仓库信息
	 * @param depotInfoItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult deleteDepotInfo(Map<String, Object> params) {
		List<SysDepotInfo> depotInfoList = JSON.parseArray(params.get("depotInfoItems").toString(), SysDepotInfo.class);
		if (depotInfoList == null || depotInfoList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysDepotInfo depotInfo : depotInfoList) {
			depotInfoMapper.deleteDepotInfo(depotInfo);

			//删除物流方式梯度和计重明细列表
			LogisticsMmodeGrads logisticsMmodeGrads2 = new LogisticsMmodeGrads();
			logisticsMmodeGrads2.setLogisticsMmodeGuid(depotInfo.getGuid());

			logisticsMmodeGradsMapper.deleteByLogisticsMmodeGuid(logisticsMmodeGrads2);
		}
		return AjaxResult.success("操作成功");
	}

	/**
	 * 通过guid修改仓库信息
	 * @param depotInfoItems
	 * @return MessageModel
	 * @author lwl
	 */
	@Override
	public AjaxResult updateDepotInfoByGuid(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		SysDepotInfo depotInfo = JSON.parseObject(params.get("depotInfoItems").toString(), SysDepotInfo.class);
		if (depotInfo == null ) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		int count = depotInfoMapper.countByCode(depotInfo);
		if (count != 0){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return AjaxResult.error("仓库编码重复,请核实再操作！");
		}

		depotInfo.setUpdateUser(ShiroUtils.getUserId());
		depotInfo.setUpdateTime(new Date());
		depotInfoMapper.updateByGuid(depotInfo);

		//删除物流方式梯度和计重明细列表
		LogisticsMmodeGrads logisticsMmodeGrads2 = new LogisticsMmodeGrads();
		logisticsMmodeGrads2.setLogisticsMmodeGuid(depotInfo.getGuid());

		logisticsMmodeGradsMapper.deleteByLogisticsMmodeGuid(logisticsMmodeGrads2);

		//判断仓储费梯度列表是否正确
		List<LogisticsMmodeGrads> infoItemsZj = JSON.parseArray(params.get("infoItems").toString(), LogisticsMmodeGrads.class);
		List<LogisticsMmodeGrads> infoItemsList;
		List<LogisticsMmodeGrads> infoItemsList1;
		for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
			if (logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads.getEndSection()) >= 0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return AjaxResult.error("开始值不允许小于等于结束值");
			}
			infoItemsList = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
					&& logisticsMmodeGrads.getStartSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
					&& !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
			).collect(Collectors.toList());
			infoItemsList1 = infoItemsZj.stream().filter(logisticsMmodeGrads1 -> logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getStartSection()) >= 0
					&& logisticsMmodeGrads.getEndSection().compareTo(logisticsMmodeGrads1.getEndSection()) <= 0
					&& !logisticsMmodeGrads.getGuid().equals(logisticsMmodeGrads1.getGuid())
			).collect(Collectors.toList());
			if (infoItemsList.size() != 0 || infoItemsList1.size() != 0){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return AjaxResult.error("区间有重叠");
			}
		}

		List<LogisticsMmodeGrads> logisticsMmodeInfoItemsList = new ArrayList<>();
		for (LogisticsMmodeGrads logisticsMmodeGrads : infoItemsZj){
			logisticsMmodeGrads.setGuid(RandomValueUtils.getGUID());
			logisticsMmodeGrads.setLogisticsMmodeGuid(depotInfo.getGuid());
			logisticsMmodeGrads.setType("CCFTD");
			logisticsMmodeInfoItemsList.add(logisticsMmodeGrads);
		}
		//新增仓储费梯度
		logisticsMmodeGradsMapper.insertLogisticsMmodeGradsBatch(logisticsMmodeInfoItemsList);

		return AjaxResult.success(MessageUtils.message("module.success.issuccess"),depotInfo.getGuid());
	}
	
}
